package com.our.sys.project1.service;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.our.set.entity.User;
import com.our.set.service.CommonService;
import com.our.sys.project1.RunConstants;
import com.our.sys.project1.dao.DiscountPriceDao;
import com.our.sys.project1.dao.ReadyCodeDao;
import com.our.sys.project1.dao.ReadySendDao;
import com.our.sys.project1.entity.DiscountPrice;
import com.our.sys.project1.entity.ReadyCode;
import com.our.sys.project1.entity.ReadySend;

@Service
@Transactional
public class DiscountPriceService {

    private static Logger logger = LoggerFactory.getLogger(DiscountPriceService.class);

    @Autowired
    private ReadyCodeDao readyCodeDao;
    @Autowired
    private ReadySendDao readySendDao;
    @Autowired
    private DiscountPriceDao discountPriceDao;
    @Autowired
    private CommonService commonService;

    /**
     * 存储划价
     * @param readySendId
     * @param strs 存放高值耗材，格式为:id,price,amount-id,price,amount
     * @throws Exception 
     */
    public void saveDiscount(Long readySendId, String strs, User u, BigDecimal allMoney) throws Exception {
        try {
            ReadySend rs = readySendDao.findOneBy("id", readySendId);
            if (null != rs) {
                DiscountPrice dp = new DiscountPrice();
                dp.setReadySendId(readySendId);
                dp.setTime(commonService.getNowString());
                dp.setWho(u.getName());
                dp.setPrice(allMoney);
                discountPriceDao.save(dp);
                if (StringUtils.isNotBlank(strs)) {
                    String[] sStrs = strs.split("-");
                    for (String str : sStrs) {
                        String[] eveStr = str.split(",");
                        String id = eveStr[0];
                        String price = eveStr[1];
                        String amount = eveStr[2];

                        ReadyCode readyCode = readyCodeDao.findOneBy("id", Long.valueOf(id));
                        readyCode.setAmount(Long.valueOf(amount));
                        readyCode.setPrice(new BigDecimal(price));
                        readyCode.setReadyCodeState(RunConstants.READY_PRICE);
                        readyCodeDao.save(readyCode);
                    }
                }
                rs.setState(RunConstants.READY_DISCOUNT);
                readySendDao.save(rs);
            } else {
                logger.error("未找到该条记录！");
                throw new Exception("未找到该条记录！");
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void deleteSend(Long readySendId, User u) throws Exception {
        ReadySend rs = readySendDao.findOneBy("id", readySendId);
        try {
            
            List<ReadyCode> rcList = readyCodeDao.find("from ReadyCode where readySendId = ?", readySendId);
            
            for (ReadyCode rc : rcList) {
                rc.setReadySendId(null);
                rc.setReadyCodeState(RunConstants.READY_GOODS);
            }

            readySendDao.delete(rs);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

}
